﻿using IR.Common.Enums;
using IR.Controller.ModelControllers;
using IR.WPF.ViewModels.LandingViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace IR.WPF.UI.ViewModels
{
    public class VMIdeaEdit : ViewModelBase
    {
        private CIdea _ideaToEdit;
        private string _typeOfPage;
        private bool _isOld;
        private ICommand _cancelCommand;
        private ICommand _saveCommand;

        public VMIdeaEdit(CIdea ideaToEdit = null)
        {
            if (ideaToEdit == null)
            {
                _ideaToEdit = new CIdea();
                _isOld = false;
                _typeOfPage = "New Idea";
            }
            else
            {
                _ideaToEdit = ideaToEdit;
                _isOld = true;
                _typeOfPage = "Edit Idea";
            }
        }

        #region **Properties**
        public string TypeOfPage
        {
            get { return _typeOfPage; }
            set { _typeOfPage = value; }
        }

        public string UserCreated
        {
            get { return _ideaToEdit.UserCreated.Username; }
            set { _ideaToEdit.UserCreated = new CUser(value); }
        }

        public string Version
        {
            get { return _ideaToEdit.Version.VersionID; }
            set { _ideaToEdit.Version = new CVersion(value); }
        }

        public string Title
        {
            get { return _ideaToEdit.Title; }
            set { _ideaToEdit.Title = value; }
        }

        public Status Status
        {
            get { return _ideaToEdit.Status; }
            set { _ideaToEdit.Status = value; }
        }

        public string Description
        {
            get { return _ideaToEdit.Description; }
            set { _ideaToEdit.Description = value; }
        }

        public string UserAssigned
        {
            get { return _ideaToEdit.UserAssigned.Username; }
            set { _ideaToEdit.UserAssigned = new CUser(value); }
        }

        public Category Category
        {
            get { return _ideaToEdit.Category; }
            set { _ideaToEdit.Category = value; }
        }

        public IEnumerable<string> UsersAvailable
        {
            get
            {
                return CUser.QueryUsers(null).Select(u => u.Username);
            }
        }

        public IEnumerable<string> VersionsAvailable
        {
            get
            {
                return CVersion.QueryVersions(null).Select(v => v.VersionID);
            }
        }

        public IEnumerable<Status> StatusTypeValues
        {
            get
            {
                return Enum.GetValues(typeof(Status))
                    .Cast<Status>();
            }
        }

        public IEnumerable<Category> CategoryTypeValues
        {
            get
            {
                return Enum.GetValues(typeof(Category))
                    .Cast<Category>();
            }
        }
        #endregion

        #region **Commands**
        /// <summary>
        /// Gets a command defined for Edit
        /// </summary>
        public ICommand Cancel
        {
            get
            {
                if (_cancelCommand == null)
                    _cancelCommand = new RelayCommand(p => CancelFunction(p as Control), p => CanCancel(p as Control));

                return _cancelCommand;
            }
        }

        /// <summary>
        /// Gets a command defined for Edit
        /// </summary>
        public ICommand Save
        {
            get
            {
                if (_saveCommand == null)
                    _saveCommand = new RelayCommand(p => SaveFunction(p as Control), p => CanSave(p as Control));

                return _saveCommand;
            }
        }
        #endregion

        #region **Command Functions**
        protected void CancelFunction(Control sender)
        {
            // Get a reference to the Window that contained the sender Control
            Window window = Window.GetWindow(sender);

            window.Close();
        }

        protected void SaveFunction(Control sender)
        {
            //bool canSave = true;
            //bool saved = false;
            //bool inputError = false;

            //if (_isOld == false)
            //{
            //    _versionToEdit.DateCreated = System.DateTime.Now;
            //}
            //else
            //{
            //    if (initialPK != _versionToEdit.VersionID)
            //        canSave = false;
            //}

            //if (_versionToEdit.Status == Status.badvalue
            //    || string.IsNullOrEmpty(_versionToEdit.VersionID))
            //{
            //    inputError = true;
            //    MessageBox.Show("Versions must have a name and real status.");
            //}

            //if (inputError == false)
            //{
            //    if (canSave == false)
            //    {
            //        MessageBox.Show("You can't change the VersionID.");
            //    }
            //    else
            //    {
            //        if (_isOld == false)
            //        {
            //            saved = _versionToEdit.Save();
            //        }
            //        else
            //        {
            //            saved = _versionToEdit.Save(true);
            //        }

            //        // Get a reference to the Window that contained the sender Control
            //        if (saved == false)
            //        {
            //            MessageBox.Show("This was not saved properly.");
            //        }
            //        else
            //        {
            //            Window window = Window.GetWindow(sender);

            //            window.Close();
            //        }
            //    }
            //}
        }
        #endregion

        #region **CanRun Functions**
        protected bool CanCancel(Control sender)
        {
            return true;
        }

        protected bool CanSave(Control sender)
        {
            return true;
        }
        #endregion
    }
}
